/*******************************************************************************
 * Copyright (c) 2002 Boris Pruessmann and others. All rights reserved. This
 * program and the accompanying materials are made available under the terms of
 * the Common Public License v0.5 which accompanies this distribution, and is
 * available at http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: Boris Pruessmann - Initial implementation.
 ******************************************************************************/
package net.sourceforge.perforce.ui.views;

import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.events.TreeEvent;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Widget;

/**
 * @version $Revision: 488 $
 * @author <a href="mailto:bpruessmann@users.sourceforge.net">Boris
 *         Pruessmann</a>
 */
class InternalTreeViewer extends TreeViewer {
  // ---------------------------------------------------------------------------
  // Private Variables

  /** Stores the only IInternalTreeViewerListener reference. */
  private IInternalTreeViewerListener internalTreeViewerListener =
      new InternalTreeViewerListenerAdapter();

  // -------------------------------------------------------------------------
  // Public Constructors

  /**
   * Constructor for InternalTreeViewer.
   * 
   * @param parent the parent composite.
   * @param styles styles.
   */
  public InternalTreeViewer(Composite parent, int styles) {
    super(parent, styles);
  }

  //----------------------------------------------------------------------------
  // -- Public Methods

  /**
   * Finds the item that equals the given item.
   * 
   * @param item the item.
   * @return the real item.
   */
  public Object findRealItem(Object item) {
    Widget widget = findItem(item);
    if (widget != null) {
      return widget.getData();
    }

    return null;
  }

  /**
   * Called to set the one and only IInternalTreeViewerListener.
   * 
   * @param listener the listener to set.
   */
  public void setInternalTreeViewerListener(IInternalTreeViewerListener listener) {
    internalTreeViewerListener = listener;
  }

  // ---------------------------------------------------------------------------
  // Protected Methods

  @Override
  protected void handleTreeExpand(TreeEvent event) {
    internalTreeViewerListener.beforeTreeExpanded(event);
    super.handleTreeExpand(event);
    internalTreeViewerListener.afterTreeExpanded(event);
  }

  @Override
  protected void handleTreeCollapse(TreeEvent event) {
    internalTreeViewerListener.beforeTreeCollapsed(event);
    super.handleTreeCollapse(event);
    internalTreeViewerListener.afterTreeCollapsed(event);
  }
}
